'use strict';

angular.module('fbdApp.auxModule')
  .filter('highlightFilter', function() {
    return function(array, text) {

      var wrapByTag = function(input) {
        var str = '<span class="highlight">' + input + '</span>';
        return str;
      }

      var replaceText = function(start, end, input) {
        var str = '';
        var before = input.substr(0, start);
        var after = input.substr(start + end);
        str = before + wrapByTag(input.substr(start, end)) + after;
        return str;
      }

      var res = text;
      var currentInput;
      // var restInput;
      for (var i = 0; i < array.length; i++) {
        if (i == 0) {
          res = text.substr(0, array[0].location[0]);
        }
        // 非最后一个
        if (i != array.length - 1) {

          currentInput = text.slice(array[i].location[0], array[i + 1].location[
            0]);
          // restInput = restInput.substr(array[i + 1].location[0]);

        } else {
          currentInput = text.substr(array[i].location[0]);
        }
        res += replaceText(0, array[i].location[1], currentInput);

        // text = replaceText(array[i].location[0], array[i].location[1], text);
        // console.log(text);
      }
      // res = text;
      return res;
    }
  });
